package com.athu.study.config;

import com.athu.study.service.ChatAssistant;
import com.athu.study.service.ChatMemoryAssistant;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.memory.chat.TokenWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiTokenCountEstimator;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LLMConfig {
    @Bean
    public ChatModel chatModelQwen(){
        return OpenAiChatModel.builder().
                apiKey(System.getenv("aliQwen-api")).
                modelName("qwen-long").
                baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1").build();
    }
    @Bean
    public ChatAssistant chatAssistant(ChatModel chatModel){
        return AiServices.create(ChatAssistant.class,chatModel);
    }

    @Bean(name = "chatMessageWindowChatMemory")
    public ChatMemoryAssistant chatMessageWindowChatMemory(ChatModel chatModel){
        return AiServices.builder(ChatMemoryAssistant.class).
                chatModel(chatModel).
                chatMemoryProvider(memoryId->
                    MessageWindowChatMemory.withMaxMessages(100)).build();
    }
    @Bean(name = "chatTokenWindowChatMemory")
    public ChatMemoryAssistant chatTokenWindowChatMemory(ChatModel chatModel){
        OpenAiTokenCountEstimator openAiTokenCountEstimator = new OpenAiTokenCountEstimator("gpt-4");
        return AiServices.builder(ChatMemoryAssistant.class).
                chatModel(chatModel).
                chatMemoryProvider(memoryId->
                        TokenWindowChatMemory.withMaxTokens(1000,openAiTokenCountEstimator)).build();
    }
}
